var util = require("../../util/util.js")
module.exports = function(uid,room){
	this.uid = uid
	this.room = room
	this.chair = -1
	this.lastRsult = false
	this.handCards = []
	this.handRests = []
	var logicBasic = require("../logic/hunanRunQuicklyLogic.js")
	var logic = new logicBasic()
	var local = {}
	var timerList = {}
	//销毁
	this.destroy = function() {
		// console.log("robot destroy : ",timer)
		for(var i in timerList){
			clearTimeout(timerList[i])
		}
	}
	//接收数据
	this.onMessage = function(notify) {
		// console.log("onMessage : ")
		// console.log(notify)
		switch(notify.cmd){
			case "gameBegin":
				this.chair = notify.chair
				this.handCards = notify.handCard
				// console.log("handCards!!!!!!!!!!",this.handCards)
				for(var i = 0;i < this.handCards.length;i++){
					this.handRests[i] = this.handCards[i].length
				}
				if(notify.curPlayer == this.chair){
					// console.log("初始出牌")
					//主动出牌
					setTimeout(this.initiativePlay.bind(this),3500)
				}
			break
			case "nextPlay":
				if(notify.initiative){
					this.lastRsult = false
				}
				if(notify.curPlayer == this.chair){
					// console.log("出牌")
					if(notify.initiative){
						//主动出牌
						this.initiativePlay()
					}else{
						//被动出牌
						this.passivePlay()
					}
				}
			break
			case "playCard":
				if(notify.result){
					this.lastRsult = notify.result
				}
				if(notify.result){
					for(var i = 0;i < notify.result.cardList.length;i++){
						for(var j = 0;j < this.handCards[notify.chair].length;j++){
							if(notify.result.cardList[i].num == this.handCards[notify.chair][j].num && notify.result.cardList[i].type == this.handCards[notify.chair][j].type){
								this.handCards[notify.chair][j].state = false
							}
						}
					}
					this.handRests[notify.chair] -= notify.result.cardList.length
				}
			break
			case "userReady":
				//开始准备
				// console.log("开始准备")
			break
			case "settlement":
				this.sendMessage("ready",{},Math.random() * 4000 + 6000)
			break
		}
	}
	this.checkWin = function(handCard,nextHand1,nextHand2) {
		//判断必胜牌组
		var tmpArrList = logic.getBestSubarea(handCard)
		var tmpWinResult = false
		var arr = false
		var count = 0
		if(tmpArrList.length <= 1){
			arr = tmpArrList[0]
		}else{
			for(var i = 0;i < tmpArrList.length;i++){
				var tmpResult = logic.getCardType(tmpArrList[i],this.handRests[this.chair])
				if(!logic.getBestWinCardList(tmpResult,nextHand1).length && !logic.getBestWinCardList(tmpResult,nextHand2).length){
					tmpWinResult = tmpArrList[i]
				}else{
					count++
				}
			}
			if(tmpWinResult && count <= 1){
				if(!arr || arr.length < tmpWinResult.length){
					arr = tmpWinResult
				}
				// console.log("必胜！！！！！   使用必胜牌组",tmpWinResult)
			}
		}
		return arr
	}
	//主动出牌
	this.initiativePlay = function() {
		var nextPlayer = (this.chair + 1) % 3
		var nextPlayer2 = (this.chair + 2) % 3
		var arr = []
		//判断必胜牌组
		var winCard = this.checkWin(this.handCards[this.chair],this.handCards[nextPlayer],this.handCards[nextPlayer2])
		// console.log("winCard",winCard)
		if(winCard){
			// console.log("主动出牌必胜！！！！！   使用必胜牌组",winCard)
			arr = winCard
		}else{
			//下家牌少于五张时防止完牌
			if(this.handRests[nextPlayer] <= 5){
				// console.log(this.room.players[nextPlayer].uid)
				arr = logic.getRobotLowValueCard(this.handCards[this.chair],this.handRests[this.chair],this.handCards[nextPlayer],this.handRests[nextPlayer])
			}else{
				//选一个价值最低的打
				arr = logic.getLowValueCard(this.handCards[this.chair],this.handRests[this.chair])
			}
		}
		var playList = []
		for(var i in arr){
			playList.push(arr[i].index)
		}
		var time = 3000
		var rand = Math.random()
		if(rand < 0.4){
			time += 3000
		}else if(rand < 0.1){
			time += 5000
		}else if(rand < 0.01){
			time += 8000
		}
		this.sendMessage("playCard",{playList : playList},time)
	}
	//被动出牌
	this.passivePlay = function() {
		var arr = []
		var nextPlayer = (this.chair + 1) % 3
		var nextPlayer2 = (this.chair + 2) % 3
		//判断必胜牌组
		var tmpArrList = logic.getAllWinCardList(this.lastRsult,this.handCards[this.chair])
		var winCard = false
		for(var i = 0;i < tmpArrList.length;i++){
			var tmpResult = logic.getCardType(tmpArrList[i],this.handRests[this.chair])
			if(!logic.getBestWinCardList(tmpResult,this.handCards[nextPlayer]).type && !logic.getBestWinCardList(tmpResult,this.handCards[nextPlayer2]).type){
				var tmpHandCard = util.deepCopyArr(this.handCards[this.chair])
				for(var z = 0;z < tmpArrList[i].length;z++){
					for(var k = 0;k < tmpHandCard.length;k++){
						if(tmpHandCard[z].num == tmpHandCard[k].num && tmpHandCard[z].type == tmpHandCard[k].type){
							tmpHandCard[z].state = false
							break
						}
					}
				}
				winCard = this.checkWin(tmpHandCard,this.handCards[nextPlayer],this.handCards[nextPlayer2])
				if(winCard){
					winCard = tmpArrList[i]
					// console.log("被动出牌  存在必胜牌组!!!")
					break
				}
			}
		}
		if(winCard){
			arr = winCard
			// console.log("被动出牌使用必胜牌组!!!",arr)
		}else{
			if(this.handRests[nextPlayer] <= 5){
				arr = logic.getRobotBestWinCardList(this.lastRsult,this.handCards[this.chair],this.handCards[nextPlayer],this.handRests[nextPlayer])
			}else{
				//选一个价值最低的打
				arr = logic.getBestWinCardList(this.lastRsult,this.handCards[this.chair])
			}
		}
		// console.log("被动出牌,选择最佳出牌")
		// console.log("选择出牌完毕!",arr)
		var time = 3000
		var rand = Math.random()
		if(rand < 0.4){
			time += 3000
		}else if(rand < 0.1){
			time += 5000
		}else if(rand < 0.01){
			time += 8000
		}
		if(arr.length){
			var playList = []
			for(var i = 0;i <  arr.length;i++){
				playList.push(arr[i].index)
			}
			// console.log("playList : ",playList)
			this.sendMessage("playCard",{playList : playList},time)
		}else{
			this.sendMessage("giveUp",{},2500)
		}
	}
	//发送数据
	this.sendMessage = function(cmd,param,time) {
		time = (Math.random() * 0.3 + 0.7) * time
		var id = (new Date()).getTime()
		timerList[id] = setTimeout(local.timeoutFun(this.room,cmd,this.uid,param,id),time)
	}
	local.timeoutFun  = function(room,cmd,uid,param,id) {
		return function() {
			delete timerList[id]
			room.handle[cmd].call(room,uid,0,param,function() {})
		}
	}
	this.sendMessage("ready",{},5000)
}